package com.dykj.fireFighting.server.base.video.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dykj.fireFighting.api.base.video.dto.VideoLocalDto;
import com.dykj.fireFighting.api.base.video.entity.VideoLocal;
import com.dykj.fireFighting.api.base.video.service.IVideoLocalService;
import com.dykj.fireFighting.common.core.service.impl.BaseServiceImpl;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.server.base.video.mapper.VideoLocalMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;


/**
 * @Author JacksonTu
 * @Date 2022-04-08 09:53:27
 */
@Slf4j
@DubboService
@Transactional(rollbackFor = Exception.class)
public class VideoLocalServiceImpl extends BaseServiceImpl<VideoLocalMapper, VideoLocal> implements IVideoLocalService {


    @Override
    public PageVo<VideoLocal> queryVideoList(VideoLocalDto videoLocalDto, LoginUserVo loginUserVo) {
        Map<String, Object> params = new HashMap<>();
        if (2 == loginUserVo.getEnterpriseType()) {
        } else if (0 == loginUserVo.getEnterpriseType()) {
            //videoLocal.setUnitId(loginUserVo.getEnterpriseId().toString());
        }

        //params.put("unitId", videoLocal.getUnitId());
        params.put("videoName", videoLocalDto.getVideoName());
        params.put("enterpriseName", videoLocalDto.getEnterpriseName());
        Page page = new Page();
        // 设置当前页码
        page.setCurrent(videoLocalDto.getPage());
        // 设置页大小
        page.setSize(videoLocalDto.getLimit());
        return new PageVo<VideoLocal>(this.baseMapper.queryVideoList(page, params));
    }

    @Override
    public void inset(String enterpriseName, String result, String uploadFolder, String accessPrefixUrl) {

        if (!StringUtils.endsWith(accessPrefixUrl, "/")) {
            accessPrefixUrl += '/';
        }
        String path = uploadFolder + enterpriseName +"/" +result;
        String url =  accessPrefixUrl + path;
        String name = result.split("\\\\")[1];
        VideoLocal videoLocal =new VideoLocal();
        videoLocal.setId(UUIDUtils.get32UUID());
        videoLocal.setVideoName(name);
        videoLocal.setVideoPath(path);
        videoLocal.setVideoUrl(url);
        videoLocal.setEnterpriseName(enterpriseName);
        videoLocal.setCreateTime(new Date());
        this.baseMapper.insert(videoLocal);
    }

}
